package com.zz.action;

import com.zz.dto.HouRentDto;
import com.zz.dto.PageDto;
import com.zz.service.HouRentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class HouRentAction {
    @Autowired
    private HouRentService houRentService;

    //分页显示列表
    @RequestMapping("/listHouRent.do")
    public ModelAndView listHouRent(HttpServletRequest request) {
        ModelAndView modelAndView=new ModelAndView();
        int currentPage=1;
        //得到当前页
        String page = request.getParameter("page");
        if (StringUtils.isEmpty(page)){
            currentPage=1;
        }else{
            currentPage=Integer.parseInt(page);
        }
        if (currentPage<=1){
            currentPage=1;
        }
        //将当前页放入dto传到service，查询列表和总页数
        PageDto pto=new PageDto();
        pto.setCurrentPage(currentPage);
        List<HouRentDto> byPage = houRentService.findByPage(pto);
        //得到总页数
        int MaxPage=pto.getMaxPage();
        if (currentPage>=MaxPage){
            currentPage=MaxPage;
        }

        //将当前页，总页数，列表数据传到前端
        modelAndView.addObject("byPage1",byPage);
        modelAndView.addObject("currentPage",currentPage);
        modelAndView.addObject("MaxPage",MaxPage);
        modelAndView.setViewName("list");
        return modelAndView;
    }
}
